<template>
  <div id="articleList">
    <!-- {{channel.name}} -->
    <van-pull-refresh
      v-model="isRefreshLoading"
      @refresh="onRefresh"
      :success-text="successText"
      :success-duration="1500"
    >
      <van-list
        v-model="loading"
        :finished="finished"
        finished-text="没有更多了"
        :error.sync="error"
        error-text="请求失败，点击重新加载"
        @load="onLoad"
      >
        <!-- 循环单文章组件 -->
        <articleItem
          v-for="(item, i) in list"
          :key="i"
          :article="item"
        ></articleItem>
      </van-list>
    </van-pull-refresh>
  </div>
</template>

<script>
// 导入api
import { getArticleList } from '@/api/article'
// 导入文章组件
import articleItem from '@/components/articleItem'
export default {
  //   props: ['channel'],
  name: 'ArticleList',
  props: {
    channel: {
      type: Object, // 指定类型
      required: true
    }
  },
  data() {
    return {
      list: [],
      loading: false,
      finished: false,
      timestamp: null,
      error: false,
      isRefreshLoading: false,
      successText: '刷新成功'
    }
  },
  methods: {
    async onLoad() {
      //   // 异步更新数据
      //   // setTimeout 仅做示例，真实场景中一般为 ajax 请求
      //   setTimeout(() => {
      //     for (let i = 0; i < 10; i++) {
      //       this.list.push(this.list.length + 1)
      //     }

      //     // 加载状态结束
      //     this.loading = false

      //     // 数据全部加载完成
      //     if (this.list.length >= 40) {
      //       this.finished = true
      //     }
      //   }, 1000)

      // 1. 发起axios请求
      try {
        const {
          data: { data }
        } = await getArticleList({
          channel_id: this.channel.id,
          timestamp: this.timestamp || Date.now(), // 时间戳，请求新的推荐数据传当前的时间戳，请求历史推荐传指定的时间戳
          with_top: 1
        })
        // console.log(data)
        this.list.push(...data.results)
        // 2.本次加载状态结束
        this.loading = false
        // 3.数据全部加载完成 根据results的长度来判断
        if (data.results.length > 0) {
          // 如果有值 更改时间戳,用于下一次请求
          this.timestamp = data.pre_timestamp
        } else {
          // 如果没值
          this.finished = true
        }
      } catch (error) {
        this.error = true
        this.finished = true
      }
    },
    // 下拉刷新
    async onRefresh() {
      // 发送请求,拿到数据
      try {
        const {
          data: { data }
        } = await getArticleList({
          channel_id: this.channel.id,
          timestamp: Date.now(), // 下拉刷新,每次都是最新的时间戳
          with_top: 1
        })
        // console.log(data)
        // 将数据置顶
        if (data.results.length > 0) {
          this.list.unshift(...data.results)
          // 设置刷新提示
          this.isRefreshLoading = false
          this.successText = `刷新成功,共${data.results.length}条数据`
        } else {
          this.isRefreshLoading = false
          this.successText = '没有更新的数据了'
        }
      } catch (error) {
        this.isRefreshLoading = false
        this.successText = '刷新失败'
      }
    }
  },
  components: {
    articleItem
  },
  created() {}
}
</script>

<style lang="less" scoped>
// 记住滚动的位置
// 添加在根组件上
#articleList {
  height: 79vh;
  overflow-y: auto;
}
</style>
